

from django.utils.safestring import mark_safe
from django.template import Library
import re

register =Library()


@register.inclusion_tag("rbac/menu.html")
def get_menu_styles(request):
    permission_menu_dict = request.session.get("permission_menu_dict")
    print("permission_menu_dict",permission_menu_dict)

    # 只显示菜单列表下对应的权限
    for val in permission_menu_dict.values():   # 只循环值

        for item in val["children"]:    # 拿children下的权限
            val["class"] = "hide"

            # ret = re.search("^{}$".format(item['url']),request.path)
            if request.show_id == item["pk"]:
                val["class"] = ""


    return {"permission_menu_dict":permission_menu_dict}

@register.filter
def has_permission(btn_url,request):
    # 校验权限
    permission_names = request.session.get("permission_names")

    return btn_url in permission_names


'''
@register.inclusion_tag("rbac/menu.html")
def get_menu_style(request):
    permission_menu_list = reqeust.session.get("permission_menu_list")
    for item in permission_menu_list:
        if re.search("^{}$".format(item["url"]),request.path):
            item["class"] = "active"

    return {"permission_menu_list":permission_menu_list}
'''

'''
   元数据：

    [  {
           'permissions__url': '/customer/list/',
           'permissions__title': '客户列表',
           'permissions__menu__title': '信息管理',
           'permissions__menu__icon': 'fa fa-connectdevelop',
           'permissions__menu__pk': 1
       },  

       {
           'permissions__url': '/mycustomer/',
           'permissions__title': '我的私户',
           'permissions__menu__title': '信息管理',
           'permissions__menu__icon': 'fa fa-connectdevelop',
           'permissions__menu__pk': 1
       },  

       {
           'permissions__url': '/payment/list/',
           'permissions__title': '缴费列表',
           'permissions__menu__title': '财务管理',
           'permissions__menu__icon': 'fa fa-code-fork',
           'permissions__menu__pk': 2
       }, ]


   目标数据：

   {
      1:{
           "title":"信息管理",
           "icon":"",
           "children":[
               {
                 "title":"客户列表",
                 "url":"",
                 "pk":1,
               }
           ]

         },

      2:{
           "title":"财务管理",
           "icon":"",
           "children":[
               {
                 "title":"缴费列表",
                 "url":"",
               },
           ]

         }, 


   }




 permission_menu_dict= {


        1:{
           "title":"信息管理",
           "icon":"",
           "children":[
               {
                 "title":"客户列表",
                 "url":"",
                 "pk": 1,
               },
                {
                 "title":"我的私户",
                 "url":"",
                },
           ]

         },

         2:{
           "title":"财务管理",
           "icon":"",
           "children":[
               {
                 "title":"缴费列表",
                 "url":"",
                 "pk: 5,
               },
           ]

         }, 

      }  




   '''
